package com.qf.flash.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import com.qf.flashlib.config.Constants;
import com.qf.flashlib.sharedpreferences.SavePermanentSharePrefs;
import com.qf.flash.event.WXPayRespEvent;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;


/**
 * 微信支付回调Activity
 */
public class WXPayEntryActivity extends AppCompatActivity implements IWXAPIEventHandler
{
    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        initData();
    }

    private void initData()
    {
        String appId = SavePermanentSharePrefs.getInstance().getDataString(Constants.WX_APP_ID);
        if (!TextUtils.isEmpty(appId))
        {
            api = WXAPIFactory.createWXAPI(this, appId);
            api.handleIntent(getIntent(), this);
        }
    }

    @Override
    protected void onNewIntent(Intent intent)
    {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq)
    {

    }

    /**
     * 回调的结果
     */
    @Override
    public void onResp(BaseResp baseResp)
    {
        if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX)
        {
            EventBus.getDefault().post(new WXPayRespEvent(baseResp));
            finish();
        }
    }
}
